﻿using System;
using ContentAppBase.Common;
using ContentAppBase.Model.Bing;
using ContentAppBase.ViewModel;
using Windows.System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace ContentAppBase.View
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class SectionPage : LayoutAwarePage
    {
        public SectionPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var vm = (MainViewModel)this.DataContext;

            if (e.Parameter.ToString() == "image")
            {
                pageTitle.Text = vm.ImageResultTitle;
                vm.GetImageResultsCommand.Execute(null);
            }
            else if (e.Parameter.ToString() == "web")
            {
                pageTitle.Text = vm.WebResultTitle;
                vm.GetWebResultsCommand.Execute(null);
            }
        }

        private void GridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem is ImageResult)
                Launcher.LaunchUriAsync(new Uri(((ImageResult)e.ClickedItem).SourceUrl, UriKind.Absolute));
            else if (e.ClickedItem is WebResult)
                Launcher.LaunchUriAsync(new Uri(((WebResult)e.ClickedItem).Url, UriKind.Absolute));
        }
    }
}
